package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"strings"
)

func main() {
	NewClient(":1234", "1")
}

func NewClient(address string, id string) {

	conn, err := net.Dial("tcp", address)

	if err != nil {
		fmt.Printf("dial address %s failed, err = %v \n", address, err)
		return
	}

	reader := bufio.NewReader(os.Stdin)
	data := make([]byte, 1024)
	go func() {
		for {
			n, e := conn.Read(data)
			if e != nil {
				fmt.Print(e)
				break
			}
			content := string(data[:n])
			fmt.Printf("\n【provider】-> %s\n", content)
		}
	}()

	for {
		fmt.Print("【client】-> ")
		text, _ := reader.ReadString('\n')
		text = strings.Replace(text, "\n", "", -1)
		_, err := conn.Write([]byte(text))
		if err != nil {
			fmt.Printf("send message failed, err = %v \n", err)
		}
		//fmt.Printf("send provider[%s] message success, len = %d \n", address, n)
	}

}
